package cn.usersmsg.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


public class Logout extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doPost(req, resp);
	}
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String user = req.getParameter("user");
		HttpSession session = req.getSession();
		session.invalidate();
		if ("user".equals(user)) {
			Cookie[] cookies = req.getCookies();
			if(cookies!=null){
		    	for(int i=0;i<cookies.length;i++){
		    		if("cname".equals(cookies[i].getName())){
		    			cookies[i].setMaxAge(0);
		    			resp.addCookie(cookies[i]);
		    			break;
		    		}
		    	}
		    }		
			resp.sendRedirect("home.jsp");
		}else{
			resp.sendRedirect("adminLogin.jsp");
		}
		
	}
}
